package com.company.mvvm.viewmodel;

import android.annotation.SuppressLint;
import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.company.mvvm.beans.ArticleBean;
import com.company.mvvm.repository.ArticleRepository;
import com.company.network.WanAndroidApi;
import com.company.network.bean.BaseResp;
import com.company.network.observe.BaseObserve;
import com.google.gson.Gson;
import com.rxjava.rxlife.RxLife;
import com.rxjava.rxlife.ScopeViewModel;

public class ArticleViewModel extends ScopeViewModel {

    public MutableLiveData<ArticleBean> articleData = new MutableLiveData<ArticleBean>();

    private final static String TAG = "ArticleViewModel";

    private final ArticleRepository repository = new ArticleRepository();

    public ArticleViewModel(@NonNull Application application) {
        super(application);
    }

    @SuppressLint("CheckResult")
    public void getArticle() {
        BaseObserve<BaseResp<ArticleBean>> baseObserve = new BaseObserve<BaseResp<ArticleBean>>() {
            @Override
            public void onSuccess(BaseResp<ArticleBean> resp) {
                //Log.e(TAG, new Gson().toJson(resp));
                articleData.postValue(resp.getData());
            }

            @Override
            public void onFail(Throwable e) {
                Log.e(TAG, e.getMessage());
            }
        };


        repository.listArticle().compose(WanAndroidApi.getInstance().applySchedulers(baseObserve,this));
    }


}
